package com.momo.mal.anime.overall;

import net.momo.mal.R;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.actionbarsherlock.view.SubMenu;
import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;

public class AnimeOverall extends SherlockActivity {
	private AnimeOverallAdapter adapter;
	private ViewPager pager;
	private PageIndicator indicator;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.anime_overall);
		setTitle("My Anime List");

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		new RequestAnimeList(this, 1).execute();

	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		SubMenu subMainMenu = menu.addSubMenu("Sort By");
//		subMainMenu.add("By Alphabetic")
//				.setOnMenuItemClickListener(new OnMenuItemClickListener() {
//
//					@Override
//					public boolean onMenuItemClick(MenuItem item) {
//						AnimeTabWatching fragmentWathcing = (AnimeTabWatching) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":0");
//						AnimeTabOnHold fragmentOnHold = (AnimeTabOnHold) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":2");
//						AnimeTabPlanToWatch fragmentPlanToWatch = (AnimeTabPlanToWatch) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":4");
//						if (fragmentWathcing != null && fragmentWathcing.getView() != null) {
//							fragmentWathcing.updateData(1);
//						}
//						if (fragmentOnHold != null && fragmentOnHold.getView() != null) {
//							fragmentOnHold.updateData(1);
//						}
//						if (fragmentPlanToWatch != null && fragmentPlanToWatch.getView() != null) {
//							fragmentPlanToWatch.updateData(1);
//						}
//						return false;
//					}
//				});
//		subMainMenu.add("By Priority")
//				.setOnMenuItemClickListener(new OnMenuItemClickListener() {
//
//					@Override
//					public boolean onMenuItemClick(MenuItem item) {
//						AnimeTabWatching fragmentWathcing = (AnimeTabWatching) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":0");
//						AnimeTabOnHold fragmentOnHold = (AnimeTabOnHold) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":2");
//						AnimeTabPlanToWatch fragmentPlanToWatch = (AnimeTabPlanToWatch) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":4");
//						if (fragmentWathcing != null && fragmentWathcing.getView() != null) {
//							fragmentWathcing.updateData(2);
//						}
//						if (fragmentOnHold != null && fragmentOnHold.getView() != null) {
//							fragmentOnHold.updateData(2);
//						}
//						if (fragmentPlanToWatch != null && fragmentPlanToWatch.getView() != null) {
//							fragmentPlanToWatch.updateData(2);
//						}
//						
//						return false;
//					}
//				});
//
//		MenuItem subMenu1Item = subMainMenu.getItem();
//		subMenu1Item.setIcon(R.drawable.ic_menu_sort_by_size);
//		subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
//				| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
//
//		return super.onCreateOptionsMenu(menu);
//	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private class RequestAnimeList extends AsyncTask<String, Void, JSONObject> {
		private ProgressDialog dialog;
		private int sort;

		public RequestAnimeList(Context cxt, int sort) {
			this.dialog = new ProgressDialog(cxt);
			this.sort = sort;
		}

		@Override
		protected void onPreExecute() {
			dialog.setMessage("Loading Data ...");
			dialog.show();
		}

		@Override
		protected JSONObject doInBackground(String... params) {

			return null;
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			dialog.dismiss();
			adapter = new AnimeOverallAdapter(getSupportFragmentManager(), sort);
			
			pager = (ViewPager) findViewById(R.id.pager);
			pager.setAdapter(adapter);

			indicator = (TitlePageIndicator) findViewById(R.id.indicator);
			indicator.setViewPager(pager);
		}

	}
}
